package cn.bearspark.api.service.main.impl;

import cn.bearspark.api.service.main.VideoService;
import cn.bearspark.api.vo.main.user.UserSlimVO;
import cn.bearspark.api.vo.main.video.PlayVideoVO;
import cn.bearspark.api.vo.main.video.PublishVideoVO;
import cn.bearspark.api.vo.main.video.VideoListVO;
import cn.bearspark.common.dto.Page;
import cn.bearspark.common.util.ConvertBeanUtil;
import cn.bearspark.common.util.TimeInfoUtil;
import cn.bearspark.framework.web.starter.context.BSRequestContext;
import lombok.Data;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author f
 */
@Service
public class VideoServiceImpl implements VideoService {

    // TODO 之后需要放到新的模块中
    private int idGenerator = 1;
    private final List<VideoPO> mapper = new ArrayList<>();

    @Data
    private static class VideoPO {
        // 视频 id
        private Long videoId;
        // 视频文件的 MD5 值
        private String videoFileHash;
        // 封面文件的 MD5 值
        private String coverFileHash;
        // 视频标题
        private String videoTitle;
        // 视频时长信息
        private String videoDurationInfo;
        // 简介
        private String description;
        // 发布时间
        private LocalDateTime publishTime;
        // 发布者的用户 id
        private Long authorId;
    }

    {
        VideoPO videoPO = new VideoPO();
        videoPO.setVideoId(1L);
        videoPO.setAuthorId(10001L);
        videoPO.setVideoDurationInfo(TimeInfoUtil.getTimeInfo(22.00));
        videoPO.setPublishTime(LocalDateTime.now());
        videoPO.setVideoTitle("周老师立大功");
        videoPO.setDescription("汪汪队");
        mapper.add(videoPO);
    }

    @Override
    public void publishVideo(PublishVideoVO publishVideoVO) {
        long userId = BSRequestContext.getUserId();
        VideoPO videoPO = ConvertBeanUtil.convert(publishVideoVO, VideoPO.class);
        videoPO.setVideoId((long) idGenerator++);
        videoPO.setAuthorId(userId);
        videoPO.setVideoDurationInfo(TimeInfoUtil.getTimeInfo(publishVideoVO.getVideoDuration()));
        videoPO.setPublishTime(LocalDateTime.now());
        mapper.add(videoPO);
    }

    @Override
    public PlayVideoVO playVideo(long videoId) {
        VideoPO videoPO = mapper.get(0);
        PlayVideoVO playVideoVO = ConvertBeanUtil.convert(videoPO, PlayVideoVO.class);
        playVideoVO.setVideoUrl("http://192.168.1.133/bear-spark/file/download/FIFA.m3u8"/*videoPO.getVideoFileHash()*/);
        playVideoVO.setPublishTimeInfo(TimeInfoUtil.getPastTimeInfo(videoPO.getPublishTime()));
        UserSlimVO authorInfo = new UserSlimVO();
        authorInfo.setUserId(10001L);
        authorInfo.setUsername("周权");
        authorInfo.setAvatarUrl("http://192.168.1.133/bear-spark/file/download/default-avatar.gif");
        playVideoVO.setAuthorInfo(authorInfo/*videoPO.getAuthorId()*/);
        return playVideoVO;
    }

    @Override
    public Page<VideoListVO> getVideoList(int page, int size) {
//        int total = mapper.size();
//        List<VideoListVO> records = mapper.stream()
//                .skip((page - 1L) * size).limit(size)
//                .map(videoPO -> {
//                    VideoListVO videoListVO = ConvertBeanUtil.convert(videoPO, VideoListVO.class);
//                    videoListVO.setAuthorName("周权"/*videoPO.getAuthorId()*/);
//                    videoListVO.setCoverUrl("http://192.168.1.133/file/download/default-side-bar.jpg"/*videoPO.getCoverFileHash()*/);
//                    return videoListVO;
//                }).toList();
        int total = 1000;
        List<VideoListVO> records = new ArrayList<>(size);
        VideoPO videoPO = mapper.get(0);
        for (int i = 0; i < size; i++) {
            VideoListVO videoListVO = ConvertBeanUtil.convert(videoPO, VideoListVO.class);
            videoListVO.setAuthorName("周权"/*videoPO.getAuthorId()*/);
            videoListVO.setCoverUrl("http://192.168.1.133/bear-spark/file/download/default-side-bar.jpg"/*videoPO.getCoverFileHash()*/);
            records.add(videoListVO);
        }
        Page<VideoListVO> pageVO = new Page<>();
        pageVO.setTotal(total);
        pageVO.setRecords(records);
        return pageVO;
    }
}
